package com.pur.zsms.lockmanage.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.lockmanage.dao.ILockTransDao;
import com.pur.zsms.lockmanage.po.LockTransPo;
import com.pur.zsms.lockmanage.service.ILockTransService;

@Service("lockTransService")
public class LockTransServiceImpl implements ILockTransService {

	@Resource(name = "lockTransDao")
	private ILockTransDao lockTransDaoImpl;

	public ILockTransDao getLockTransDaoImpl() {
		return lockTransDaoImpl;
	}

	public void setLockTransDaoImpl(ILockTransDao lockTransDaoImpl) {
		this.lockTransDaoImpl = lockTransDaoImpl;
	}

	public boolean lockTrans(String method, int supId) throws Exception {
		// 判断此供货商是否存在
		List<LockTransPo> listPo = lockTransDaoImpl.getLockTransPo(method, supId);
		LockTransPo tLockTransPo = new LockTransPo();
		if (listPo != null && listPo.size() > 0) {
			tLockTransPo = listPo.get(0);
			if (tLockTransPo.getLockFlag() == 0) {
				return false;
			} else {
				tLockTransPo.setLockFlag(0);
				lockTransDaoImpl.updateSupplier(tLockTransPo);
			}

		} else {
			tLockTransPo.setCustId(0);
			tLockTransPo.setLockFlag(0);
			tLockTransPo.setLockName(method);
			tLockTransPo.setSupId(supId);
			lockTransDaoImpl.addSupplier(tLockTransPo);
		}

		return true;
	}

	// 供货商解锁
	public void unlockTrans(String method, int supId) throws Exception {
		LockTransPo tLockTransPo = lockTransDaoImpl.getLockTransPo(method, supId).get(0);
		tLockTransPo.setLockFlag(1);
		lockTransDaoImpl.updateSupplier(tLockTransPo);
	}

	// 客户 锁判断
	public boolean lockTransForCust(String method, long custId) throws Exception {
		// 判断此供货商客户是否存在
		List<LockTransPo> listPo = lockTransDaoImpl.getLockTransPoForCust(method, custId);
		LockTransPo tLockTransPo = new LockTransPo();
		if (listPo != null && listPo.size() > 0) {
			tLockTransPo = listPo.get(0);
			if (tLockTransPo.getLockFlag() == 0) {
				return false;
			} else {
				tLockTransPo.setLockFlag(0);
				lockTransDaoImpl.updateSupplier(tLockTransPo);
			}

		} else {
			tLockTransPo.setCustId(custId);
			tLockTransPo.setLockFlag(0);
			tLockTransPo.setLockName(method);
			tLockTransPo.setSupId(0);
			lockTransDaoImpl.addSupplier(tLockTransPo);
		}

		return true;
	}

	// 客户解锁
	public void unlockTransForCust(String method, long custId) throws Exception {
		LockTransPo tLockTransPo = lockTransDaoImpl.getLockTransPoForCust(method, custId).get(0);
		tLockTransPo.setLockFlag(1);
		lockTransDaoImpl.updateSupplier(tLockTransPo);
	}

}
